package cn.itcast.controller.cargo;

import cn.itcast.controller.BaseController;
import cn.itcast.domain.cargo.ExtCproduct;
import cn.itcast.domain.cargo.ExtCproductExample;
import cn.itcast.domain.cargo.Factory;
import cn.itcast.domain.cargo.FactoryExample;
import cn.itcast.service.cargo.ExtCproductService;
import cn.itcast.service.cargo.FactoryService;
import cn.itcast.utils.FileUploadUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.List;
import java.util.UUID;

@RequestMapping("/cargo/extCproduct")
@Controller
public class ExtCproductController extends BaseController{
    @Reference
    private ExtCproductService extCproductService;
    @Reference
    private FactoryService factoryService;
    @Autowired
    private FileUploadUtil fileUploadUtil;
    @RequestMapping(value = "/list",name = "进入到添加附件界面")
    public String list(String contractId, String contractProductId, @RequestParam(defaultValue = "1")int page,@RequestParam(value = "pageSize",defaultValue = "5")int pages){
        FactoryExample factoryExample = new FactoryExample();
        factoryExample.createCriteria().andCtypeEqualTo("附件");
        List<Factory> list = factoryService.findAll(factoryExample);
        request.setAttribute("factoryList",list);
        ExtCproductExample extCproductExample = new ExtCproductExample();
        extCproductExample.createCriteria().andContractProductIdEqualTo(contractProductId);
        PageInfo pageInfo = extCproductService.findAll(extCproductExample,page,pages);
        request.setAttribute("page",pageInfo);
        request.setAttribute("contractId",contractId);
        request.setAttribute("contractProductId",contractProductId);
        return "cargo/extc/extc-list";
    }
    @RequestMapping(value = "/edit",name = "更新和条件操作")
    public String edit(ExtCproduct extCproduct,MultipartFile productPhoto) throws Exception {
        String upload=null;
        try {
            if(productPhoto!=null){
                upload = fileUploadUtil.upload(productPhoto);
            }
        } catch (Exception e) {
            e.printStackTrace();
            upload=null;
        }
        extCproduct.setProductImage(upload);
        if(StringUtils.isBlank(extCproduct.getId())){
            /*UUID.randomUUID().toString()*/
            extCproduct.setId(UUID.randomUUID().toString());
            extCproduct.setCompanyId(getCompanyId());
            extCproduct.setCompanyName(getCompanyName());
            extCproduct.setCreateTime(new Date());
            extCproductService.save(extCproduct);
        }else {
            extCproductService.update(extCproduct);
        }
        return "redirect:/cargo/extCproduct/list.do?contractId="+extCproduct.getContractId()+"&contractProductId="+extCproduct.getContractProductId();
    }
    @RequestMapping(value = "/delete",name = "删除")
    public String delete(String id,String contractId,String contractProductId){
        extCproductService.delete(id);
        return "redirect:/cargo/extCproduct/list.do?contractId="+contractId+"&contractProductId="+contractProductId;
    }
    @RequestMapping(value = "toUpdate",name = "跳转到更新界面")
    public String toUpdate(String id){
        ExtCproduct extCproduct = extCproductService.findById(id);
        request.setAttribute("extCproduct",extCproduct);
        FactoryExample factoryExample = new FactoryExample();
        factoryExample.createCriteria().andCtypeEqualTo("附件");
        List<Factory> factoryList = factoryService.findAll(factoryExample);
        request.setAttribute("factoryList",factoryList);
        request.setAttribute("contractProductId",extCproduct.getContractProductId());
        request.setAttribute("contractId",extCproduct.getContractId());
        return "cargo/extc/extc-update";
    }
}
